package cat.joronya.supermanager.team;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import cat.joronya.supermanager.R;
import cat.joronya.supermanager.drawer.BaseActionBarActivity;
import cat.joronya.supermanager.team.MyTeamDetailFragment.OnProgressListener;

/**
 * My team detail activity shows some fields and list of
 * players.
 * @author pol
 */
public class MyTeamDetailActivity extends BaseActionBarActivity implements OnProgressListener
{
	public static final String TAG = "Supervermut";
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
    	// set the layout for content view
    	setmLayoutResource(R.layout.fragment_layout);
        setmLayoutId(R.id.fragment_layout);
    	super.onCreate(savedInstanceState);
    	
    	// recuperem id i position del detall passat
    	long id = getIntent().getLongExtra("cat.joronya.supermanager.myteams.idequ",0);
		int position = getIntent().getIntExtra("cat.joronya.supermanager.myteams.position", 0);
		
    	// si canviem a landscape i tenim dual pane, llavors llançar PrivateLeagueActivity
    	if( isTablet() && getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE )
    	{
    		// private league list activity
			Intent myTeamsListActivity = new Intent(getBaseContext(), MyTeamsListActivity.class);
			myTeamsListActivity.putExtra("cat.joronya.supermanager.myteams.idequ", id);
			myTeamsListActivity.putExtra("cat.joronya.supermanager.myteams.position", position);
    		startActivity(myTeamsListActivity);
    		overridePendingTransition(0,0);
    		finish();
    	}
    	
    	// setejem el fragment q toca
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                       .replace(R.id.fragment, MyTeamDetailFragment.newInstance(id))
                       .commit();
        
        // titol de la pantalla home inicalment
        setTitle(getResources().getString(R.string.my_teams_tab));
    }
    
    @Override
    protected void onNewIntent(Intent intent) 
    {
    	super.onNewIntent(intent);
    }
    
    // show progress dialog with message and cancelable status
	public void showProgress(String message, boolean cancelable) 
	{
		ActionProgressDialogFragment newFragment = ActionProgressDialogFragment.newInstance(message, cancelable);
	    newFragment.show(getSupportFragmentManager(), "progress_dialog");
	    newFragment.setCancelable(cancelable);
	}

	// dismiss progress dialog if its available
	public void dismissProgress() 
	{
		ActionProgressDialogFragment dialog = (ActionProgressDialogFragment)getSupportFragmentManager().findFragmentByTag("progress_dialog");
		if( dialog != null )
			dialog.dismissAllowingStateLoss();
	} 
	
	@Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {
		// Handle item selection
        switch (item.getItemId()) 
        {
        	// Respond to the action bar's Up/Home button
        	case android.R.id.home:
        		Intent upIntent = NavUtils.getParentActivityIntent(this);
                NavUtils.navigateUpTo(this, upIntent);
                return true;
        	default:
        		return super.onOptionsItemSelected(item);
        }
    }
    
}